package com.klhappy.houyuan.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.klhappy.houyuan.R;
import com.klhappy.houyuan.activity.AddLocationActivity;
import com.klhappy.houyuan.interfaces.DialogInterface;
import com.klhappy.houyuan.utils.NotifyMethod;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by MrPu on 16/1/8.
 */
public class AddLoctionDialog {
    private Context context;
    private Dialog dialog;
    private PoiSearch mPoiSearch;
    private List<String> list;

    @InjectView(R.id.seach_edit)
    EditText seach_edit;
    @InjectView(R.id.search_list_dialog)
    ListView lv_search;

    @OnClick(R.id.search_image)
    public void search_image(){
        seach_edit.setText("");
    }

    public AddLoctionDialog(Context context) {
        this.context = context;
        mPoiSearch = PoiSearch.newInstance();
        list=new ArrayList<String>();
    }

    public void setDialog(){
        View view = LayoutInflater.from(context).inflate(R.layout.activity_dialog ,null);
        ButterKnife.inject(this, view);

        seach_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!"".equals(s.toString())) {
                    mPoiSearch.searchInCity((new PoiCitySearchOption())
                            .city("成都")
                            .keyword(s.toString())
                            .pageCapacity(20)
                            .pageNum(0));
                }
            }
        });

        mPoiSearch.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() {
            @Override
            public void onGetPoiResult(PoiResult poiResult) {
                list.clear();
                if (poiResult.getAllPoi() != null) {
                    for (int i = 0; i < poiResult.getAllPoi().size(); i++) {
                        list.add(poiResult.getAllPoi().get(i).address);
                    }
                    lv_search.setAdapter(new MyAdapter());
                }
            }

            @Override
            public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {
                Log.e("this", poiDetailResult.toString());
            }
        });

        lv_search.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                NotifyMethod.getInstance().postNotification(AddLocationActivity.AVG, list.get(position));
                Dismiss();
            }
        });
        dialog = new Dialog(context, R.style.Dialog_Fullscreen);
        dialog.setContentView(view);
    }

    public void Dismiss(){
        if (dialog.isShowing()){
            dialog.dismiss();
            mPoiSearch.destroy();
        }
    }

    public void Show(){
        dialog.show();
    }

    public class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            ViewHolder holder;
            if(view == null){
                view = LayoutInflater.from(context).inflate(R.layout.activity_dialog_search, null);
                holder = new ViewHolder();
                holder.textView=(TextView) view.findViewById(R.id.dialog_text);

                view.setTag(holder);
            } else {
                holder = (ViewHolder)view.getTag();
            }

            holder.textView.setText(list.get(position).toString());
            return view;
        }

        class ViewHolder{
            TextView textView;
        }
    }
}
